package com.csi.sai.bl.dataview;

import java.util.ArrayList;

import javax.sql.DataSource;

import com.csi.sai.data.entities.Demand;
import com.csi.sai.utilities.Utilities;
import com.csi.sai.utilities.templates.reports.DemandTemplate;
import com.csi.sai.utilities.templates.reports.RecruitmentTemplate;

public class GetDataViewRecruitment extends GetDataView {
	
	private String htmlHeader;
	private String htmlBody;
	
	public GetDataViewRecruitment(DataSource ds) {
		super(ds);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void writeHeaders() {
		htmlHeader = RecruitmentTemplate.headers;
	}

	@Override
	protected <T> void writeBody(Generics<T> object) {
		StringBuffer sb = new StringBuffer();
		ArrayList<Demand> rsDemands = (ArrayList<Demand>)object.getObject(); 
		for(int index=0; index<rsDemands.size(); index++) {
			Demand demand = (Demand)rsDemands.get(index);
			String htmlRow = RecruitmentTemplate.row;
			
			String actualRow = htmlRow.replace("{title}", demand.getIdDemand()+"-"+demand.getClientName()+"-"+demand.getVacancy()).
								replace("{demand}", ""+demand.getIdDemand()).
								replace("{client}", demand.getClientName()).
								replace("{status}", demand.getStatus()).
								replace("{salesperson}", demand.getSalesPerson()).
								replace("{contact}", demand.getContact()).
								replace("{vacancy}", demand.getVacancy()).
								replace("{profile}", demand.getProfile()).
								replace("{creation}", Utilities.getDateByFormat(demand.getCreation(), Utilities.formatView)).
								replace("{priority}", demand.getPriorityDate()!=null ? Utilities.getDateByFormat(demand.getPriorityDate(), Utilities.formatView) : "").
								replace("{duration}", ""+demand.getDuration()).
								replace("{rate}", ""+demand.getMensualRate()).
								replace("{finish}", Utilities.getDateByFormat(demand.getFinshDate(), Utilities.formatView)).
								replace("{novacancy}", ""+demand.getNumVacancy()).
								replace("{days}", ""+Utilities.getDaysFromADate(demand.getAuthorization())).
								replace("{index}", ""+index);
			
			if(demand.getStatus().equals("En Espera") || demand.getStatus().equals("Reasignada")) {
				actualRow = actualRow.replace("{buttons}", RecruitmentTemplate.buttons.replace("{demand}", ""+demand.getIdDemand()));
				actualRow = actualRow.replace("{img}", RecruitmentTemplate.vacancyOption.replace("{demand}", ""+demand.getIdDemand()).
																							replace("{display}", "none"));
			} else if(demand.getStatus().equals("Cancelada")) {
				actualRow = actualRow.replace("{buttons}", "");
				actualRow = actualRow.replace("{img}", RecruitmentTemplate.vacancyOption.replace("{demand}", ""+demand.getIdDemand()).
						replace("{display}", "none"));
			} else {
				actualRow = actualRow.replace("{buttons}", "");
				actualRow = actualRow.replace("{img}", RecruitmentTemplate.vacancyOption.replace("{demand}", ""+demand.getIdDemand()).
																							replace("{display}", "inline"));
			}
			sb.append(actualRow);
		}
		htmlBody = RecruitmentTemplate.body.replace("{body}", sb.toString());
	}

	@Override
	public String getHTMLCode() {
		return htmlHeader.replace("{body}", htmlBody);
	}

}
